package net.piggy.core.socket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import net.piggy.core.session.dubbo.DubboGatewaySessionFactory;
import net.piggy.core.socket.handler.GatewayServerHandler;
import net.piggy.core.socket.handler.RequestForwardHandler;

public class GatewayChannelInitializer extends ChannelInitializer<SocketChannel> {


    private final DubboGatewaySessionFactory gatewaySessionFactory;

    public GatewayChannelInitializer(DubboGatewaySessionFactory gatewaySessionFactory) {

        this.gatewaySessionFactory = gatewaySessionFactory;
    }

    @Override
    protected void initChannel(SocketChannel channel) {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast(new HttpRequestDecoder());
        pipeline.addLast(new HttpResponseEncoder());
        pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
        pipeline.addLast(new GatewayServerHandler());
        pipeline.addLast(new RequestForwardHandler(gatewaySessionFactory));
    }
}
